package com.ah.service;


import com.ah.common.result.R;
import com.ah.common.service.IAccountService;
import com.ah.common.service.IOrderService;
import com.ah.entity.TOrder;
import com.ah.mapper.TOrderMapper;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

@DubboService
public class OrderService implements IOrderService {

    @Autowired
    private TOrderMapper orderMapper;

    @DubboReference
    private IAccountService accountService;

    /**
     * 创建订单
     * @param userId 用户id
     * @param commodityCode 商品sku
     * @param orderCount 商品数量
     * @param money 总金额
     * @return
     */
    @Override
    public R createOrder(String userId, String commodityCode, int orderCount, int money) {

        //扣减金额
        accountService.debit(userId,money);

        TOrder order = new TOrder();
        order.setUserId(userId);
        order.setMoney(money);
        order.setCount(orderCount);
        order.setCommodityCode(commodityCode);
        int row = orderMapper.insert(order);
        if(row == 1){
            return R.success("创建订单成");
        }
        return  R.error("下单失败，服务异常");
    }
}
